#ifndef _CANARY2_NET_LISTENER_H_
#define _CANARY2_NET_LISTENER_H_

#include <utility>
#include <coroutine>

#include "../coro/epoll_suspended_manager.h"
#include "../sys/fd_guard.h"

#include "sock.h"
#include "tcp.h"

namespace wilson::net
{
    class listener
    {
    public:
        listener(socket& sock);
        ::std::pair<bool, tcp_conn> accept_nonblk();

        // reference: N4770 Coroutine TS
        class accept_awtb
        {
        public:
            accept_awtb(
                coro::epoll_suspend_manager& esm, 
                listener& lsn
            ) : aa_esm{ esm }, 
                aa_listener{ lsn }
            {
            }

            bool await_ready() noexcept;
            void await_suspend(::std::coroutine_handle<> h);
            tcp_conn await_resume();

        private:
            coro::epoll_suspend_manager& aa_esm;
            listener& aa_listener;
            tcp_conn aa_result;
            int aa_errno{};
        };

        accept_awtb coro_accept(coro::epoll_suspend_manager&);

    private:
        socket& l_sock;
    };
}

#endif
